package mule;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 * Map class, JPanel of Tile buttons that allows the user to interact with the
 * game
 */
public class Map extends JPanel implements Serializable {
	static boolean selectionPhase = true;
	static boolean landOfficePhase = false;
	private static ArrayList buttons = new ArrayList();
	static Tile[] standardMap = { new Plain(), new Plain(), new Mountain1(),
			new Plain(), new River(), new Plain(), new Mountain3(),
			new Plain(), new Plain(), new Plain(), new Mountain1(),
			new Plain(), new Plain(), new River(), new Plain(), new Plain(),
			new Plain(), new Mountain3(), new Mountain3(), new Plain(),
			new Plain(), new Plain(), new Town(), new Plain(), new Plain(),
			new Plain(), new Mountain1(), new Plain(), new Mountain2(),
			new Plain(), new Plain(), new River(), new Plain(),
			new Mountain2(), new Plain(), new Plain(), new Plain(),
			new Plain(), new Mountain2(), new Plain(), new River(),
			new Plain(), new Plain(), new Plain(), new Mountain2() };
	static ArrayList<Tile> totalOwnedTiles = new ArrayList<Tile>();

	/**
	 * Return the StandardMap Array of tiles
	 * 
	 * @return Tile[]
	 */
	public static Tile[] getMap() {
		return standardMap;
	}

	/**
	 * return Tile by Pint
	 * 
	 * @param x
	 * @return Tile t
	 */
	public static Tile getTile(Point x) {
		for (Tile t : standardMap) {
			if (t.getPoint().equals(x)) {
				return t;
			}
		}
		return null;
	}

	/**
	 * Return JButtons on map and their points
	 * 
	 * @return
	 */
	public static ArrayList<JButton> getButtons() {
		return buttons;
	}

	/**
	 * Construct a Map with dimensions x by y
	 */
	public Map(int x, int y) {
		setLayout(new GridLayout(x, y));
		int xcoord = 0;
		int ycoord = 0;
		for (int i = 0; i < x * y; i++) {
			if (xcoord == 9) {
				xcoord = 0;
				ycoord++;
			}
			standardMap[i].setPoint(new Point(xcoord, ycoord));
			JButton button = new JButton();
			String type = standardMap[i].toString();
			if (type == "Plain") {
				button.setIcon(new ImageIcon("src/mule/images/plain.png"));
			}
			else if (type == "1 Mountain") {
				button.setIcon(new ImageIcon("src/mule/images/mountain1.png"));
			}
			else if (type == "2 Mountain") {
				button.setIcon(new ImageIcon("src/mule/images/mountain2.png"));
			}
			else if (type == "3 Mountain") {
				button.setIcon(new ImageIcon("src/mule/images/mountain3.png"));
			}
			else if (type == "River") {
				button.setIcon(new ImageIcon("src/mule/images/river.png"));
			}
			else if (type == "Town") {
				button.setIcon(new ImageIcon("src/mule/images/town.png"));

			}
			button.setMargin(new Insets(0,0,0,0));
			
			
			add(button);
			buttons.add(button);
			button.putClientProperty("number", i);
			button.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					JButton btn = (JButton) e.getSource();
					int spot = (int) btn.getClientProperty("number");
					Tile clickedItem = standardMap[spot];
					Player player = Game.getCurrentPlayer();
					Game.resetPasses();
					if (clickedItem.toString() == "Town") {
						new TownFrame();
					} else if (!selectionPhase && player.hasMule()
							&& (!clickedItem.getOwner().equals(player) || clickedItem
									.hasMule())) {
						System.out
								.println("Lost mule because of incorrect property or property already has a mule");
						player.setMule(null);
					}
					/*
					 * else if (selectionPhase == false &&
					 * !clickedItem.getOwner().equals(player)) {
					 * System.out.println("lost mule"); player.setMule(null); }
					 * else if (selectionPhase == false &&
					 * !clickedItem.getOwner().equals(player) &&
					 * !clickedItem.hasMule()) {
					 * System.out.println("lost mule"); player.setMule(null); }
					 */
					else if (!selectionPhase && player.hasMule()
							&& clickedItem.getOwner().equals(player)
							&& !clickedItem.hasMule()) {
						System.out.println("Placed mule");
						clickedItem.setMule(player.getMule());
						btn.setText("<html><center>" + btn.getText() + ": "
								+ player.getName() + "'s " + player.getMule()
								+ "</center></html>");
						player.setMule(null);
					} else if ((!clickedItem.isOwned() && selectionPhase
							&& player.getMoney() >= clickedItem.getCost()) ||
							(!clickedItem.isOwned() && landOfficePhase
							&& player.getMoney() >= clickedItem.getCost())) {
						if (landOfficePhase) {
							player.setMoney(player.getMoney()
									- clickedItem.getCost());
							btn.setBackground(player.getColor());
							clickedItem.setOwned();
							clickedItem.setOwner(player);
							player.addTile(clickedItem);
							totalOwnedTiles.add(clickedItem); 
						}
						else if (Game.roundNumber <= 2) {
							btn.setBackground(player.getColor());
							clickedItem.setOwned();
							clickedItem.setOwner(player);
							player.addTile(clickedItem);
							totalOwnedTiles.add(clickedItem);	
						} else if (Game.roundNumber > 2) {
							player.setMoney(player.getMoney()
									- clickedItem.getCost());
							btn.setBackground(player.getColor());
							clickedItem.setOwned();
							clickedItem.setOwner(player);
							player.addTile(clickedItem);
							totalOwnedTiles.add(clickedItem); 
						}
						if (!landOfficePhase) {
							Game.switchPlayer();
							
						}
						Game.bottomPanel.updateInfo();
					}

				}
			});
			xcoord++;
		}
	}

	/**
	 * End the land selection phase, phase after has not been implemented yet
	 */
	public void selectionPhaseOff() {
		selectionPhase = false;
		JOptionPane.showMessageDialog(null, "End of Land Selection Phase");
		Game.roundNumber = 1;
	}
}